package com.chen.spring.cloud.service.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.BaseController;
import com.chen.entity.SignIn;
import com.chen.entity.User;
import com.chen.spring.cloud.service.admin.service.ISignInService;
import com.chen.spring.cloud.service.admin.service.IZsUserService;
import com.chen.vo.WxCode2Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 陈永鹏
 * @since 2019-03-25
 */
@RestController
@RequestMapping("/user")
public class ZsUserController extends BaseController {

    @Autowired
    private IZsUserService zsUserService;

    @Autowired
    private ISignInService signInService;

    @RequestMapping("/getUidByOpenid")
    public User getUidByOpenid(String wx_openid){
        return zsUserService.getOne(new QueryWrapper<User>().eq("wx_openid",wx_openid));
    }


    @RequestMapping("/signIn")
    public Integer signIn(String r_id,Integer u_id){
        SignIn signIn = new SignIn();
        signIn.setU_id(u_id);
        signIn.setR_id(Integer.parseInt(r_id));
        signIn.setSign_in_time((int)(System.currentTimeMillis() / 1000));
        signInService.save(signIn);
        return signIn.getSi_id();
    }

    @RequestMapping("/addUser")
    public User addUser(WxCode2Session wxCode2Session){
        User user = zsUserService.getOne(new QueryWrapper<User>().eq("wx_openid",wxCode2Session.getOpenid()));
        if (user == null){
            user.setU_img(wxCode2Session.getU_img());
            user.setU_name(wxCode2Session.getU_name());
            user.setU_nickname(wxCode2Session.getU_nickname());
            user.setWx_openid(wxCode2Session.getOpenid());
            user.setUnionid(wxCode2Session.getUnionid());
            user.setU_phone(wxCode2Session.getU_phone());
            zsUserService.save(user);
        } else {
            user.setU_img(wxCode2Session.getU_img());
            user.setU_name(wxCode2Session.getU_name());
            user.setU_nickname(wxCode2Session.getU_nickname());
            user.setWx_openid(wxCode2Session.getOpenid());
            user.setUnionid(wxCode2Session.getUnionid());
            user.setU_phone(wxCode2Session.getU_phone());
            zsUserService.updateById(user);
        }
        return user;
    }

}
